<?php

class PublicAction extends Action {
    //程序初始化
    /**
     * 头部和底部数据获取
     * @author 沈威(修改)
     * @tel 15005164276
     * @email 403382132@qq.com
     * Enter description here ...
     */
	public function _initialize(){
		
		$this->assign("is_login",UserAction::checkLogin());
		$confinfo = M('Config')->find();
		$map['status'] = 1;
		$artlist = M('Confart')->field('arid,artitle')->where($map)->select();
		$artnum = count($artlist);
		$this->assign('artnum',$artnum);
		$this->assign('artlist',$artlist);
		$this->assign("_basetitle_",$confinfo["title"]);
		$this->assign("_desc_",$confinfo["content"]);
		$this->assign("_key_",$confinfo["sitekey"]);
		//获取搜索的分类
		$category = M('Category');
		$map['cateid']=array('LT',10);
		$sclist=$category->where($map)->order('cateid asc')->select();
		$this->assign("sclist",$sclist);
		//获取热门关键字
		$keyword = M('Keyword');	
		$kap['category']=0;	
		$keyArr = $keyword->where($kap)->order("orders asc")->select();
		$this->assign('keyArr',$keyArr);

    }
    
    /**
     * 全站9个栏目的搜索
     * @author 沈威
     * @tel 15005164276
     * @email 403382132@qq.com
     */
    public function category_search($view)
    {
    	import("ORG.Util.Page");
		$lap['is_dele'] = 0;
		$lap['status'] = 1;
		$cate = M('Category');
		if($_POST)
		{
			$bything=$_POST['bything'];
			if($bything==1)
			{
				$keyword=$_POST['keyword1'];
			}
			else 
			{
				$keyword=$_POST['keyword2'];
			}
			
			if(isset($_SESSION['zkeyword']))
			{
				array_push($_SESSION['zkeyword'],$keyword);
				$_SESSION['zkeyword']=array_unique($_SESSION['zkeyword']);
			}
			else 
			{
				
				$zkeyword=array();
				array_push($zkeyword,$keyword);
				$_SESSION['zkeyword']=$zkeyword;
			}
			$this->assign('zkeywords',$_SESSION['zkeyword']);
			

			$stime=$_POST['stime'];

			if($stime==1)
			{
				$btime_p=$_POST['btime'];
				$etime_p=$_POST['etime'];
				$btime=strtotime($_POST['btime']);
				$etime=strtotime($_POST['etime']);
				$ptime=strtotime($_POST['etime'])+86400;
				$ztime=strtotime($_POST['btime'])+86400;
				if($btime!=''&&$etime!='')
				{
					if($btime==$etime)
					{
						$lap['uploadtime']=array(array('gt',$btime),array('lt',$ptime));
					}
					elseif($btime>$etime)
					{
						$ntime=$btime;
						$btime=$etime;
						$etime=$ntime;
						$lap['uploadtime']=array(array('gt',$btime),array('lt',$ztime)) ;
					}
					else 
					{
						$lap['uploadtime']=array(array('gt',$btime),array('lt',$ptime)) ;
					}
				}
				else 
				{
					if(!($btime==''&&$etime==''))
					{
						if($btime=='')
						{
							$lap['uploadtime']=array('lt',$ptime);
						}
						else 
						{
							$lap['uploadtime']=array('gt',$btime);
						}
					}
				}
			}
			else 
			{

				$day=$_POST['ftime'];
				switch ($day)
				{
					case 2:$lap['uploadtime']=array(array('gt',time()-86400),array('lt',time())) ;break;
					case 3:$lap['uploadtime']=array(array('gt',time()-172800),array('lt',time())) ;break;
					case 4:$lap['uploadtime']=array(array('gt',time()-604800),array('lt',time())) ;break;
					case 5:$lap['uploadtime']=array(array('gt',time()-1296000),array('lt',time())) ;break;
				}
			}
			
			$bything=$_POST['bything'];
			if($bything==1)
			{
				$keyword=$_POST['keyword1'];
			}
			else 
			{
				$keyword=$_POST['keyword2'];
			}
			
			
			
			
			//title属性
			if($_POST['submit']==1)
			{
				if($bything==1)
				{
					$lap['title']=array('like','%'.$keyword.'%');
					$where['code']  = $keyword;
					$where['title']  = $lap['title'];
					$where['_logic'] = 'or';
					$lap['_complex'] = $where;
					unset($lap['title']);
				}
				else 
				{
					$lengh=strlen($keyword);
					$num=0;
					if($keyword!="")
					{
						$lap['title']=array();
						while($num<$lengh)
						{
							$zi=mb_substr($keyword,$num,3);
							array_push($lap['title'],array('like','%'.$zi.'%'));
							$num+=3;
						}
					}
				}
			}
			else 
			{
				if($bything==1)
				{
					if($keyword=="")
					{
						unset($lap['title']);
						unset($lap['code']);
					}
					else
					{
						$lap['title']=array('like','%'.$keyword.'%');
						$where['code']  = $keyword;
						$where['title']  = $lap['title'];
						$where['_logic'] = 'or';
						
						unset($lap['title']);
						if($_SESSION['lastkey']!="")
						{
							$where['code']  = $keyword;
							$where['_string']= " (title like '%".$keyword."%')  AND ( title like '%".$_SESSION['lastkey']."%') ";
							$where['_logic'] = 'or';
						}
						$lap['_complex'] = $where;
					}
				}
				else 
				{
					$keyword.=$_SESSION['lastkey'];
					$lengh=strlen($keyword);
					$num=0;
					if($keyword!="")
					{
						$lap['title']=array();
						while($num<$lengh)
						{
							$zi=mb_substr($keyword,$num,3);
							array_push($lap['title'],array('like','%'.$zi.'%'));
							$num+=3;
						}
					}
				}
				
			}
			
			//完全匹配
			$all_key=$_POST['all_key'];
			
			//全库搜索
			$all_sql=$_POST['all_sql'];
			
			if($all_key==1)
			{
				if($keyword!="")
				{
					$lap['title']=$keyword;
				}
			}
			if($all_sql==2)
			{
				unset($lap['is_dele']);
				unset($lap['status']);
			}
			
			$order=$_POST['orderby'];
			switch ($order)
			{
				case 1:$orderby="uploadtime desc";break;
				case 2:$orderby="uploadtime asc";break;
				case 3:$orderby="is_hot desc,uploadtime desc";break;
			}
			$pgnum = $_POST['count'];
			$_SESSION['lastkey']=$keyword;
			
		}
		else 
		{
			if(count($_GET)==1)
			{
				unset($_GET);
			}
			else 
			{
				$lap=$_GET;
				unset($lap['PHPSESSID']);
				unset($lap['_URL_']);
				if($_GET['pgnum'])
				{
					$pgnum=$_GET['pgnum'];
				}
				if($_GET['key']&&$_GET['key']!='')
				{
					$keyword=$_GET['key'];
					$lap['title']=array('like','%'.$keyword.'%');
					$where['code']  = $keyword;
					$where['title']  = $lap['title'];
					$where['_logic'] = 'or';
					$lap['_complex'] = $where;
					unset($lap['title']);
				}
				if($_GET['stime'])
				{
					$stime=$_GET['stime'];
		
					if($stime==1)
					{
						$btime_p=$_GET['btime'];
						$etime_p=$_GET['etime'];
						$btime=strtotime($_GET['btime']);
						$etime=strtotime($_GET['etime']);
						$ptime=strtotime($_GET['etime'])+86400;
						$ztime=strtotime($_GET['btime'])+86400;
						if($btime!=''&&$etime!='')
						{
							if($btime==$etime)
							{
								$lap['uploadtime']=array(array('gt',$btime),array('lt',$ptime));
							}
							elseif($btime>$etime)
							{
								$ntime=$btime;
								$btime=$etime;
								$etime=$ntime;
								$lap['uploadtime']=array(array('gt',$btime),array('lt',$ztime)) ;
							}
							else 
							{
								$lap['uploadtime']=array(array('gt',$btime),array('lt',$ptime)) ;
							}
						}
						else 
						{
							if(!($btime==''&&$etime==''))
							{
								if($btime=='')
								{
									$lap['uploadtime']=array('lt',$ptime);
								}
								else 
								{
									$lap['uploadtime']=array('gt',$btime);
								}
							}
						}
					}
					else 
					{
						$day=$_GET['ftime'];
						switch ($day)
						{
							case 2:$lap['uploadtime']=array(array('gt',time()-86400),array('lt',time())) ;break;
							case 3:$lap['uploadtime']=array(array('gt',time()-172800),array('lt',time())) ;break;
							case 4:$lap['uploadtime']=array(array('gt',time()-604800),array('lt',time())) ;break;
							case 5:$lap['uploadtime']=array(array('gt',time()-1296000),array('lt',time())) ;break;
						}
					}
				}
				
				//完全匹配
				if($_GET['all_key'])
				{
					$all_key=$_GET['all_key'];
				}
				//全库搜索
				if($_GET['all_sql'])
				{
					$all_sql=$_GET['all_sql'];
				}
				if($all_key==1)
				{
					if($keyword!="")
					{
						$lap['title']=$keyword;
					}
				}
				if($all_sql==2)
				{
					unset($lap['is_dele']);
					unset($lap['status']);
				}
				
				$order=$_GET['orderby'];
				switch ($order)
				{
					case 1:$orderby="uploadtime desc";break;
					case 2:$orderby="uploadtime asc";break;
					case 3:$orderby="is_hot desc,uploadtime desc";break;
				}
				unset($lap['orderby']);
				unset($lap['pgnum']);
				unset($lap['all_key']);
				unset($lap['all_sql']);
				unset($lap['p']);
			}
		}
		
		if($_GET['cateid']&&$_GET['erji'])
		{
			unset($lap);
			$cap['catepid']=$_GET['cateid'];
			$cat_list=$cate->where($cap)->select();
			$cat_str="";
			foreach ($cat_list as $k=>$v)
			{
				$cat_str.=$v['cateid'].',';
			}
			$cat_str=substr($cat_str,0,strlen($cat_str)-1);
			if($cat_str!='')
			{
				$cat_str=','.$cat_str;
			}
			$lap['cateid']=array('in',$_GET['cateid'].$cat_str);
		}
		if($_GET['cateid']&&!$_GET['erji'])
		{
			unset($lap);
			$lap['cateid']=$_GET['cateid'];
			$lap['is_dele'] = 0;
			$lap['status'] = 1;
		}
		if(!$pgnum)
		{
			$pgnum=15;
		}
		if(!$orderby)
		{
			$orderby='uploadtime desc';
		}
		
		unset($lap['key']);
		unset($lap['ftime']);
		unset($lap['stime']);
		unset($lap['btime']);
		unset($lap['etime']);
		//$pgnum=2;
		$count = $view->where($lap)->count();
		$page = new Page($count,$pgnum);
		//分页跳转的时候保证查询条件
		 foreach($lap as $key=>$val) {
		
			 if(is_array($val))
			{
				$page->parameter   .=   "key=".$keyword."&";
			}
			else 
			{
				$page->parameter   .=   "$key=".urlencode($val)."&";
			}
		
		}
    	if($day)
		{
			$page->parameter   .=  "ftime=".$day."&";
		}
		if($stime)
		{
			$page->parameter   .=  "stime=".$stime."&";
		}
    if($btime)
		{
			$page->parameter   .=  "btime=".$btime_p."&";
		}
    if($etime)
		{
			$page->parameter   .=  "etime=".$etime_p."&";
		}
		if($all_key)
		{
			$page->parameter   .=  "all_key=".$all_key."&";
		}
    	if($all_sql)
		{
			$page->parameter   .=  "all_sql=".$all_sql."&";
		}
		$page->parameter   .=  "orderby=".$order."&";
		$page->parameter   .=  "pgnum=".$pgnum;
		
		
		$list = $view->where($lap)->limit($page->firstRow.",".$page->listRows)->order($orderby)->select();
		//print_r($view->getlastsql());
		$tiaojian=$lap;
		$tiaojian['keyword1']=$keyword;
		$tiaojian['count']=$pgnum;
		$tiaojian['ftime']=$day;
		$tiaojian['stime']=$stime;
		$tiaojian['btime']=$btime_p;
		$tiaojian['etime']=$etime_p;
		$tiaojian['all_key']=$all_key;
		$tiaojian['all_sql']=$all_sql;
		$tiaojian['orderby']=$order;
		$this->assign('choose',$tiaojian);
		$this->assign("page",$page->show());
		return $list;
    }
}